package com.tuananh.radnom.model.netservice;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.tuananh.radnom.model.NeValidator;
import com.tuananh.radnom.model.netpoller.IPollParser;
import com.tuananh.radnom.model.netpoller.NetPoller;
import com.tuananh.radnom.model.netpoller.NetPollerConcrete;
import com.tuananh.radnom.model.netpoller.RadnomRRD;
import com.tuananh.radnom.model.thresholding.AbstractThresholder;

@XmlRootElement(name = "netservice")
@XmlType(propOrder = { "rrd","params", "netPoller", "thresholder" })
public class NetService implements Serializable, NeValidator {

	Long id;
	/**
	 * The unique name of the service
	 */

	String name;

	/**
	 * interval
	 */
	long interval;

	/**
	 * List of parameter
	 */
	HashMap<String, String> params = new HashMap<String, String>();
	/**
	 * Poller for this network service
	 */
	NetPollerConcrete netPoller;

	/**
	 * Parser
	 */
	// IPollParser pollParser;
	// @XmlElementRef
	// public IPollParser getPollParser() {
	// return pollParser;
	// }
	//
	// public void setPollParser(IPollParser pollParser) {
	// this.pollParser = pollParser;
	// }
	/**
	 * Thresholder
	 */
	ArrayList<AbstractThresholder> thresholder;
	RadnomRRD rrd;

	public RadnomRRD getRrd() {
		return rrd;
	}

	public void setRrd(RadnomRRD rrd) {
		this.rrd = rrd;
	}

	@XmlAttribute(name = "interval", required = false)
	public long getInterval() {
		return interval;
	}

	public void setInterval(long interval) {
		this.interval = interval;
	}

	public ArrayList<AbstractThresholder> getThresholder() {
		return thresholder;
	}

	public void setThresholder(ArrayList<AbstractThresholder> thresholders) {
		this.thresholder = thresholders;
	}

	@XmlElement(name = "netpoller")
	public NetPollerConcrete getNetPoller() {
		return netPoller;
	}

	public void setNetPoller(NetPollerConcrete netPoller) {
		this.netPoller = netPoller;
	}

	@XmlAttribute(required = false)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@XmlAttribute
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@XmlJavaTypeAdapter(MyMapTypeAdapter.class)
	public HashMap<String, String> getParams() {
		if (params==null) params= new HashMap<String, String>();
		return params;
	}

	public void setParams(HashMap<String, String> params) {
		this.params = params;
	}

	public boolean validate(Object ne) {
		// TODO Auto-generated method stub
		NetService netS = (NetService) ne;
		if (netS.getNetPoller() != null) {
			if (!netS.getNetPoller().getName().equals(this.name))
				return false;
		}
		return true;
	}
	public void populate(NetService source){
		this.getParams().putAll(source.getParams());
		if (source.getNetPoller()!=null){
			this.netPoller = source.getNetPoller();
		}
	}
}
